Passed
Push — main ( c4701f...95102b )
by Andrii
02:26
created

source-extends-exclude.ts ➔ exclusion   A

Complexity

Conditions 2

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
dl 0
loc 25
rs 9.85
c 0
b 0
f 0
1
import { ClassValue } from "../../defs"
2
3
export {}
4
5
// type Excluder<
6
//   S extends Record<string, ClassValue>,
7
//   E extends {[K in keyof S]?: ClassValue}
8
// > = { [P in Exclude<keyof S, keyof E>]: S[P]; }
9
10
// interface Exclusion<
11
//   S extends Record<string, ClassValue>
12
// > {
13
//   (source: S, ex: {[K in keyof S]?: ClassValue}): Excluder<S, typeof ex>
14
// }
15
16
function exclusion<
17
  E extends Record<string, ClassValue>,
18
  S extends Record<keyof E, ClassValue>
19
>(
20
  source: S, ex: E
21
): { [P in Exclude<keyof S, keyof E>]: S[P]; }
22
{
23
  const $return = {...source}
24
  for (const k in ex) {
25
    delete $return[k]
26
  }
27
28
  return $return
29
}
30
31
const source: Record<"a"|"b"|"c"|"d"|"e", ClassValue> = {a: "a", b: undefined, c: "c", d: undefined, e: undefined}
32
33
const step0 = exclusion(
34
  //@ts-expect-error Property 'z' is missing in type
35
  source,
36
  {z: undefined}
37
)
38
, answ0: typeof step0 = {
39
  whatever: true
40
}
41
, step1 = exclusion(source, {a: "a", b: undefined})
42
, step2 = exclusion(step1, {"c": undefined})
43
//@ts-expect-error Property 'd' is missing
44
, answ
45
: typeof step2 = {
46
  e: "",
47
  //@ts-expect-error Object literal may only specify known properties, and 'z'
48
  z: ""
49
}
50
export {step2, answ0}